#ifndef _PACKET_H_
#define _PACKET_H_

#include <string>
#include <list>
#include <map>

class XMPPError;
class PacketExtension;

/**
 * Base class for XMPP packets. Every packet has a unique ID (which is automatically
 * generated, but can be overriden). Optionally, the "to" and "from" fields can be set,
 * as well as an arbitrary number of properties.
 *
 * Properties provide an easy mechanism for clients to share data. Each property has a
 * String name, and a value that is a Java primitive (int, long, float, double, boolean)
 * or any Serializable object (a Java object is Serializable when it implements the
 * Serializable interface).
 *
 * @author Matt Tucker
 */
class Packet {
public:
	
    /**
     * Constant used as packetID to indicate that a packet has no id. To indicate that a packet
     * has no id set this constant as the packet's id. When the packet is asked for its id the
     * answer will be <tt>null</tt>.
     */
    static std::string ID_NOT_AVAILABLE;
	
    /**
     * Returns the next unique id. Each id made up of a short alphanumeric
     * prefix along with a unique numeric value.
     *
     * @return the next id.
     */
    static std::string nextID();
	static void setDefaultXmlns(std::string & defaultXmlns);

    /**
     * Returns the unique ID of the packet. The returned value could be <tt>null</tt> when
     * ID_NOT_AVAILABLE was set as the packet's id.
     *
     * @return the packet's unique ID or <tt>null</tt> if the packet's id is not available.
     */
	std::string getPacketID();
	
    /**
     * Sets the unique ID of the packet. To indicate that a packet has no id
     * pass the constant ID_NOT_AVAILABLE as the packet's id value.
     *
     * @param packetID the unique ID for the packet.
     */
    void setPacketID(std::string & packetID);
	
    /**
     * Returns who the packet is being sent "to", or <tt>null</tt> if
     * the value is not set. The XMPP protocol often makes the "to"
     * attribute optional, so it does not always need to be set.<p>
     *
     * The StringUtils class provides several useful methods for dealing with
     * XMPP addresses such as parsing the
     * {@link StringUtils#parseBareAddress(String) bare address},
     * {@link StringUtils#parseName(String) user name},
     * {@link StringUtils#parseServer(String) server}, and
     * {@link StringUtils#parseResource(String) resource}.  
     *
     * @return who the packet is being sent to, or <tt>null</tt> if the
     *      value has not been set.
     */
	std::string getTo() {
        return to;
    }
	
    /**
     * Sets who the packet is being sent "to". The XMPP protocol often makes
     * the "to" attribute optional, so it does not always need to be set.
     *
     * @param to who the packet is being sent to.
     */
    virtual void setTo(std::string to) {
        this->to = to;
    }
	
    /**
     * Returns who the packet is being sent "from" or <tt>null</tt> if
     * the value is not set. The XMPP protocol often makes the "from"
     * attribute optional, so it does not always need to be set.<p>
     *
     * The StringUtils class provides several useful methods for dealing with
     * XMPP addresses such as parsing the
     * {@link StringUtils#parseBareAddress(String) bare address},
     * {@link StringUtils#parseName(String) user name},
     * {@link StringUtils#parseServer(String) server}, and
     * {@link StringUtils#parseResource(String) resource}.  
     *
     * @return who the packet is being sent from, or <tt>null</tt> if the
     *      value has not been set.
     */
	std::string & getFrom() {
        return from;
    }
	
    /**
     * Sets who the packet is being sent "from". The XMPP protocol often
     * makes the "from" attribute optional, so it does not always need to
     * be set.
     *
     * @param from who the packet is being sent to.
     */
    void setFrom(std::string from) {
        this->from = from;
    }
	
    /**
     * Returns the error associated with this packet, or <tt>null</tt> if there are
     * no errors.
     *
     * @return the error sub-packet or <tt>null</tt> if there isn't an error.
     */
    XMPPError * getError() {
        return error;
    }

    /**
     * Sets the error for this packet.
     *
     * @param error the error to associate with this packet.
     */
    void setError(XMPPError * error) {
        this->error = error;
    }
	
    /**
     * Returns an unmodifiable collection of the packet extensions attached to the packet.
     *
     * @return the packet extensions.
     */
	std::list<PacketExtension*> getExtensions();
	    
	/**
     * Returns the first extension of this packet that has the given namespace.
     *
     * @param namespace the namespace of the extension that is desired.
     * @return the packet extension with the given namespace.
     */
    PacketExtension * getExtension(std::string & nameSpace) {
		std::string s("");
        return getExtension(s, nameSpace);
    }
	
    /**
     * Returns the first packet extension that matches the specified element name and
     * namespace, or <tt>null</tt> if it doesn't exist. If the provided elementName is null
     * than only the provided namespace is attempted to be matched. Packet extensions are
     * are arbitrary XML sub-documents in standard XMPP packets. By default, a 
     * DefaultPacketExtension instance will be returned for each extension. However, 
     * PacketExtensionProvider instances can be registered with the 
     * {@link org.jivesoftware.smack.provider.ProviderManager ProviderManager}
     * class to handle custom parsing. In that case, the type of the Object
     * will be determined by the provider.
     *
     * @param elementName the XML element name of the packet extension. (May be null)
     * @param namespace the XML element namespace of the packet extension.
     * @return the extension, or <tt>null</tt> if it doesn't exist.
     */
    PacketExtension * getExtension(std::string & elementName, std::string & nameSpace);
	
    /**
     * Adds a packet extension to the packet.
     *
     * @param extension a packet extension.
     */
    void addExtension(PacketExtension * extension);

    /**
     * Removes a packet extension from the packet.
     *
     * @param extension the packet extension to remove.
     */
    void removeExtension(PacketExtension * extension);

    /**
     * Returns the packet property with the specified name or <tt>null</tt> if the
     * property doesn't exist. Property values that were orginally primitives will
     * be returned as their object equivalent. For example, an int property will be
     * returned as an Integer, a double as a Double, etc.
     *
     * @param name the name of the property.
     * @return the property, or <tt>null</tt> if the property doesn't exist.
     */
	std::string getProperty(std::string & name);

    /**
     * Sets a property with an Object as the value. The value must be Serializable
     * or an IllegalArgumentException will be thrown.
     *
     * @param name the name of the property.
     * @param value the value of the property.
     */
    void setProperty(std::string & name, std::string & value);
	
    /**
     * Deletes a property.
     *
     * @param name the name of the property to delete.
     */
    void deleteProperty(std::string & name);
	
    /**
     * Returns an unmodifiable collection of all the property names that are set.
     *
     * @return all property names.
     */
	std::list<std::string> getPropertyNames();

    /**
     * Returns the packet as XML. Every concrete extension of Packet must implement
     * this method. In addition to writing out packet-specific data, every sub-class
     * should also write out the error and the extensions data if they are defined.
     *
     * @return the XML format of the packet as a String.
     */
	virtual std::string toXML() = 0;
	
	std::string getXmlns() {
        return this->xmlns;
    }
	
	bool operator==( const Packet & p) const {
		return error == p.error && from == p.from && packetExtensions == p.packetExtensions && packetID == p.packetID && to == p.to && xmlns == p.xmlns;
	};
	
	virtual int hashCode() ;
protected:
	static std::string DEFAULT_LANGUAGE;
	
    /**
     * Returns the extension sub-packets (including properties data) as an XML
     * String, or the Empty String if there are no packet extensions.
     *
     * @return the extension sub-packets as XML or the Empty String if there
     * are no packet extensions.
     */
	std::string getExtensionsXML();

    static std::string parseXMLLang(std::string & language) ;

    static std::string getDefaultLanguage() ;

private:
	static std::string DEFAULT_XML_NS;
	    
	/**
     * A prefix helps to make sure that ID's are unique across mutliple instances.
     */
    static std::string prefix;
    
	/**
     * Keeps track of the current increment, which is appended to the prefix to
     * forum a unique ID.
     */
    static long id;

	std::string xmlns;
	std::string packetID;
	std::string to;
	std::string from;
	std::list<PacketExtension*> packetExtensions;

    std::map<std::string,std::string> properties;
    XMPPError * error;

};

#endif // -- _PACKET_H_
